package AS3 {
import flash.profiler.profile;

import mx.containers.Canvas;
import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.Image;
import mx.controls.Label;
import mx.controls.RadioButton;
import mx.controls.Text;
import mx.controls.TextArea;
import mx.utils.StringUtil;


public class QuizProcessor {

    private var activeBookmark:int = 1;
    private var activeQuestion:int = 1;

    private var maxQuestionNumber:int = 4;
    private var tooltipAreaInitialY:int = 50;

    private var CORRECT_ANSWER_SCORE:int = 10;
    private var WRONG_ANSWER_SCORE:int = 1;

    private var answerResponseText:String;
    private var answer:int = -1;
    private var correctAnswer:int = -2;
    private var correctAnswerAlt:int = -2;

    private var questionAnswered:Boolean = false;

    private var quizXML:XML;
    private var hintTextArea:TextArea;
    private var btnHelpSkip:Button;
    private var btnHelpNext:Button;
    private var lblHelp1:Text;
    private var cnvHelpSmall:Canvas;
    private var dimQuizImg:Image;

    private var quiz_radio1:RadioButton;
    private var quiz_radio2:RadioButton;
    private var quiz_radio3:RadioButton;
    private var quiz_radio4:RadioButton;
    private var quiz_radio5:RadioButton;

    private var quiz_radio1_label:Label;
    private var quiz_radio2_label:Label;
    private var quiz_radio3_label:Label;
    private var quiz_radio4_label:Label;
    private var quiz_radio5_label:Label;

    private var mainProcessor:MainProcessor;

    private var lang:String;

    private var scoreMessages:Object = {pl:"Otrzymales {0} punktów",en:"Your score is {0}"};
    private var correctAnswerMsg:Object = {pl:"Poprawna odpowiedz to odpowiedz nr {0}",en:"Correct answer is answer number {0}"};
    private var answerCntMessages:Object = {pl:"Poprawnych odpowiedzi: {0}  niepoprawnych odpowiedzi: {1}",en:"Correct answers {0},wrong answers {1}"};


    private var radioStates:Array = new Array(5);

    public function QuizProcessor(lang:String):void{
        this.lang = lang;
    }

    public function clickAnswered(answerNumber:int):void {
        if(questionAnswered){
           //restoreRadioState();
           return;
        }

        correctAnswer = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).wlasciwa;
        correctAnswerAlt = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).wlasciwa_alt;
        if (answerNumber == 1) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_a;
        }
        if (answerNumber == 2) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_b;
        }
        if (answerNumber == 3) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_c;
        }
        if (answerNumber == 4) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_d;
        }
        if (answerNumber >= 5 && answerNumber <= 8) {
            answerResponseText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).odp_e;
        }

        answer = answerNumber;
        var correct:Boolean = false;

        if (correctAnswerAlt == answer || correctAnswer == 0 || correctAnswer == answer) {
            mainProcessor.updateUserScore(CORRECT_ANSWER_SCORE,1,0);
            correct = true;
        }
        else {
            mainProcessor.updateUserScore(WRONG_ANSWER_SCORE,0,1);
        }

        var person:Person = mainProcessor.getCurrentPerson();
        if (correct) {
            hintTextArea.htmlText = createCorrectHtmlAnswerText(answerResponseText, person);
        }
        else {
            hintTextArea.htmlText =  createWrongHtmlAnswerText(answerResponseText,person,correctAnswer);
        }

        hintTextArea.alpha = 0.3;
        hintTextArea.y = tooltipAreaInitialY;

        questionAnswered = true;

    }


    private function createCorrectHtmlAnswerText(answerText:String,person:Person):String {
        var s:String = answerText;
        s += "<br/>";
        s += createPointsInfo(person);
        return s;
    }

    private function createWrongHtmlAnswerText(answerText:String,person:Person,correctAnswerNumber:int):String{
        var s:String = answerText;
        s += "<br/>";
        s += StringUtil.substitute(correctAnswerMsg[lang],correctAnswerNumber);
        s += "<br/>";
        s += createPointsInfo(person);
        return s;
    }

    private function createPointsInfo(person:Person):String {
        return StringUtil.substitute(answerCntMessages[lang],person.correctAnswerCnt,person.wrongAnswerCnt);
    }

    public function quiz(bookmarkNumber:int):void {
        activeBookmark = bookmarkNumber;
        btnHelpNext.visible = true;

        activeQuestion = 1;
        maxQuestionNumber = quizXML.zakladka.(numer == activeBookmark).max_pyt;

        resetQuiz();
        setViewForNextQuestion();
    }

    public function nextQuestion():void {
        activeQuestion++;
        resetQuiz();
        setViewForNextQuestion();

    }

    private function adjustToolTipAndButtons(answer:String):void {
        var toolTipNewY:int = tooltipAreaInitialY;
        var enterKeyCnt:int = countEnters(answer);

        hintTextArea.htmlText = answer;

        toolTipNewY = toolTipNewY + 40;
        hintTextArea.height = 20 + enterKeyCnt * 15;
        hintTextArea.width = 650;
        hintTextArea.y = toolTipNewY;
        hintTextArea.alpha = 0.3;

        toolTipNewY = toolTipNewY + 40 + enterKeyCnt * 15;

//        btnHelpSkip.y = toolTipNewY;
//        btnHelpNext.y = toolTipNewY;


    }

    private function setViewForNextQuestion():void {
        tooltipAreaInitialY = 50;
        maxQuestionNumber = quizXML.zakladka.(numer == activeBookmark).max_pyt;



        if(activeQuestion <= maxQuestionNumber){
            var questionText:String = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).pyt_tekst;
            var enterCounts:int = countEnters(questionText);

            lblHelp1.height = 20 + enterCounts * 15;
            tooltipAreaInitialY = tooltipAreaInitialY + enterCounts * 15;
            lblHelp1.htmlText = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).pyt_tekst;
            setPossibleAnswers();


            hintTextArea.htmlText = "";
            hintTextArea.alpha = 0.0;
            hintTextArea.y = tooltipAreaInitialY;

            if(activeQuestion == maxQuestionNumber){
               btnHelpNext.visible = false;
            }
        }

        dimQuizImg.visible = true;
        cnvHelpSmall.visible = true;
    }

    private function resetQuiz():void {
        answer = -1;
        correctAnswer = -2;
        correctAnswerAlt = -2;
        questionAnswered = false;
        if (quiz_radio1.selected) {
            quiz_radio1.selected = false;
        }
        if (quiz_radio2.selected) {
            quiz_radio2.selected = false;
        }
        if (quiz_radio3.selected) {
            quiz_radio3.selected = false;
        }
        if (quiz_radio4.selected) {
            quiz_radio4.selected = false;
        }
        if (quiz_radio5.selected) {
            quiz_radio5.selected = false;
        }

    }

    public function skipQuiz():void {
        resetQuiz();
        dimQuizImg.visible = false;
        cnvHelpSmall.visible = false;
    }

    public function showAnswer():void {
        if(answer != -1){
            adjustToolTipAndButtons(answerResponseText);
        }
        else {
            adjustToolTipAndButtons("Nie wybrano żadnej odpowiedzi");
        }
    }

    private function setPossibleAnswers():void {
        var str:String = new String("");
        var enterCounts:int = 0;
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).a != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).a;
            enterCounts = countEnters(str);
            quiz_radio1_label.y = tooltipAreaInitialY;
            quiz_radio1_label.height = 20 + enterCounts * 15;
            quiz_radio1.y = tooltipAreaInitialY + enterCounts * 7.5;
            tooltipAreaInitialY = tooltipAreaInitialY + 34 + enterCounts * 15;
            quiz_radio1_label.htmlText = str;
            quiz_radio1.visible = true;
            quiz_radio1_label.visible = true;
        }
        else {
            quiz_radio1.visible = false;
            quiz_radio1_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).b != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).b;
            enterCounts = countEnters(str);
            quiz_radio2_label.y = tooltipAreaInitialY;
            quiz_radio2_label.height = 20 + enterCounts * 15;
            quiz_radio2.y = tooltipAreaInitialY + enterCounts * 7.5;
            tooltipAreaInitialY = tooltipAreaInitialY + 34 + enterCounts * 15;
            quiz_radio2_label.htmlText = str;
            quiz_radio2.visible = true;
            quiz_radio2_label.visible = true;
        }
        else {
            quiz_radio2.visible = false;
            quiz_radio2_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).c != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).c;
            enterCounts = countEnters(str);
            quiz_radio3_label.y = tooltipAreaInitialY;
            quiz_radio3_label.height = 20 + enterCounts * 15;
            quiz_radio3.y = tooltipAreaInitialY + enterCounts * 7.5;
            tooltipAreaInitialY = tooltipAreaInitialY + 34 + enterCounts * 15;
            quiz_radio3_label.htmlText = str;
            quiz_radio3.visible = true;
            quiz_radio3_label.visible = true;
        }
        else {
            quiz_radio3.visible = false;
            quiz_radio3_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).d != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).d;
            enterCounts = countEnters(str);
            quiz_radio4_label.y = tooltipAreaInitialY;
            quiz_radio4_label.height = 20 + enterCounts * 15;
            quiz_radio4.y = tooltipAreaInitialY + enterCounts * 7.5;
            tooltipAreaInitialY = tooltipAreaInitialY + 34 + enterCounts * 15;
            quiz_radio4_label.htmlText = str;
            quiz_radio4.visible = true;
            quiz_radio4_label.visible = true;
        }
        else {
            quiz_radio4.visible = false;
            quiz_radio4_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).e != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).e;
            enterCounts = countEnters(str);
            quiz_radio5_label.y = tooltipAreaInitialY;
            quiz_radio5_label.height = 20 + enterCounts * 15;
            quiz_radio5.y = tooltipAreaInitialY + enterCounts * 7.5;
            tooltipAreaInitialY = tooltipAreaInitialY + 34 + enterCounts * 15;
            quiz_radio5_label.htmlText = str;
            quiz_radio5.visible = true;
            quiz_radio5_label.visible = true;
        }
        else {
            quiz_radio5.visible = false;
            quiz_radio5_label.visible = false;
        }

        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).f != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).f;
            //entery = liczEntery(str);
            //quiz_radio6_label.y = podstawa;
            //quiz_radio6_label.height = 20 + entery * 15;
            //quiz_radio6.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio6_label.htmlText = str;
            //quiz_radio6.visible=true;quiz_radio6_label.visible=true;
        }
        //else {quiz_radio6.visible=false;quiz_radio6_label.visible=false;}
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).g != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).g;
            //entery = liczEntery(str);
            //quiz_radio7_label.y = podstawa;
            //quiz_radio7_label.height = 20 + entery * 15;
            //quiz_radio7.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio7_label.htmlText = str;
            //quiz_radio7.visible=true;quiz_radio7_label.visible=true;
        }
        //else {quiz_radio7.visible=false;quiz_radio7_label.visible=false;}
        if (quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).h != "") {
            str = quizXML.zakladka.(numer == activeBookmark).pytanie.(numer == activeQuestion).h;
            //entery = liczEntery(str);
            //quiz_radio8_label.y = podstawa;
            //quiz_radio8_label.height = 20 + entery * 15;
            //quiz_radio8.y = podstawa + entery * 7.5;
            //podstawa = podstawa + 34 + entery * 15;
            //quiz_radio8_label.htmlText = str;
            //quiz_radio8.visible=true;quiz_radio8_label.visible=true;
        }
        //else {quiz_radio8.visible=false;quiz_radio8_label.visible=false;}
    }

    private function countEnters(tekst:String):int {
        var i:int;
        var enterCnt:int = 0;
        var charCode:int = 0;

        var s:String = "";

        return tekst.length / 85;

    }

    public function setQuizyXML(value:XML):void {
        quizXML = value;
    }

    public function setButtons(btnHelpDalej:Button, btnHelpNext:Button):void {
        this.btnHelpSkip = btnHelpDalej;
        this.btnHelpNext = btnHelpNext;
    }

    public function setHelpLabel(label:Text):void {
        this.lblHelp1 = label;
    }

    public function setDarknessImg(image:Image):void {
        this.dimQuizImg = image;
    }

    public function setCanvas(canvas:Canvas):void {
        this.cnvHelpSmall = canvas;
    }


    public function setButtonRadios(radio1:RadioButton, radio2:RadioButton, radio3:RadioButton, radio4:RadioButton, radio5:RadioButton):void {
        this.quiz_radio1 = radio1;
        this.quiz_radio2 = radio2;
        this.quiz_radio3 = radio3;
        this.quiz_radio4 = radio4;
        this.quiz_radio5 = radio5;
    }

    public function setButtonRadiosLabels(label1:Label, label2:Label, label3:Label, label4:Label, label5:Label):void {
        this.quiz_radio1_label = label1;
        this.quiz_radio2_label = label2;
        this.quiz_radio3_label = label3;
        this.quiz_radio4_label = label4;
        this.quiz_radio5_label = label5;
    }

    private function enableRadios(enabled:Boolean):void{
        quiz_radio1.enabled = enabled;
        quiz_radio2.enabled = enabled;
        quiz_radio3.enabled = enabled;
        quiz_radio4.enabled = enabled;
        quiz_radio5.enabled = enabled;
    }

    private function setRadiosVisibility(visible:Boolean):void {
        quiz_radio1.visible = visible;
        quiz_radio2.visible = visible;
        quiz_radio3.visible = visible;
        quiz_radio4.visible = visible;
        quiz_radio5.visible = visible;
    }

    private function setRadioLabelsVisibility(visible:Boolean):void {
        quiz_radio1_label.visible = visible;
        quiz_radio2_label.visible = visible;
        quiz_radio3_label.visible = visible;
        quiz_radio4_label.visible = visible;
        quiz_radio5_label.visible = visible;

    }

    public function setHintTextArea(value:TextArea):void {
        hintTextArea = value;
    }

    public function setMainProcessor(mainProcessor:MainProcessor):void {
        this.mainProcessor = mainProcessor;
    }

}
}

